package com.hq.ims.data.service;

import com.hq.ims.data.dao.LogSysAccessMapper;
import com.hq.ims.data.entity.LogSysAccess;
import com.hq.ims.data.vo.LogSysAccessMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 * @Detail 系统访问日志服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseLogSysAccessService extends BaseService<LogSysAccessMapper, LogSysAccess> {


    public List<LogSysAccess> getByPage(
            RequestPage requestPage,
            LogSysAccessMapstruct.PageSearchVo searchData
    ) {
        //根据传递的搜索参数进行检索
        RequestPage page = this.lambdaQuery()
                .like(StringUtils.isNotEmpty(searchData.getUserAccount()), LogSysAccess::getCreateUserAccount, searchData.getUserAccount())
                .like(StringUtils.isNotEmpty(searchData.getClassName()), LogSysAccess::getClassName, searchData.getClassName())
                .like(StringUtils.isNotEmpty(searchData.getMethodName()), LogSysAccess::getMetName, searchData.getMethodName())
                .ge(searchData.getBeginTime() != null, LogSysAccess::getCreateDateTime, searchData.getBeginTime())
                .le(searchData.getEndTime() != null, LogSysAccess::getCreateDateTime, searchData.getEndTime())
                .orderByDesc(LogSysAccess::getCreateDateTime)
                .page(requestPage);

        List<LogSysAccess> records = page.getRecords();
        return records;
    }

    /**
     * 清除日志成功
     * @author 7wen
     * @date 2022/4/22 10:35
     */
    public Boolean clearLogByTime(LogSysAccessMapstruct.ClearLogVo clearLogVo) {
        //通过日志id+时间区间清除日志
        return this.lambdaUpdate()
                .ge(clearLogVo.getBeginTime() != null, LogSysAccess::getCreateDateTime, clearLogVo.getBeginTime())
                .le(clearLogVo.getEndTime() != null, LogSysAccess::getCreateDateTime, clearLogVo.getEndTime())
                .remove();
    }
}
